package hot_100;

public class 除自身以外数组的乘积 {
    public static void main(String[] args) {

    }

    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] ans = new int[n];
        for (int i = 0; i < n; i++) {
            if (i - 1 >= 0)
                ans[i] = ans[i - 1] * nums[i];
            else {
                ans[i] = nums[i];
            }
        }
        if (n >= 2)
            ans[n - 1] = ans[n - 2];
        for (int i = n - 2; i >= 0; i--) {
            nums[i] = nums[i] * nums[i + 1];
            if (i - 1 >= 0)
                ans[i] = ans[i - 1] * nums[i + 1];
            else {
                ans[i] = nums[i + 1];
            }
        }
        return ans;
    }
}
